/**
 * 
 */
package com.google.code.cisx.web;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.ValidationError;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.code.cisx.gwt.client.LoginRemoteService;
import com.google.code.cisx.servlets.RemoteServiceBasedActionBeanSupport;

/**
 * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
 * @version $Id: DefyLoginActionBean.java 259 2008-11-29 13:18:52Z rahul.thakur.xdev $
 * @since 1.0
 */
@UrlBinding("/com.google.code.cisx.gwt.Desktop/defy/Login.action")
public class DefyLoginActionBean extends RemoteServiceBasedActionBeanSupport implements LoginRemoteService {

  private static final String RESULT_LOGIN_FAILED = "LOGIN FAILED!";

  private static final String RESULT_LOGIN_SUCCESSFUL = "LOGIN SUCCESSFUL!";

  private static final long serialVersionUID = -6012921899023095495L;

  private static final Log log = LogFactory.getLog(DefyLoginActionBean.class);

  // @Validate(required = true)
  private String username;

  // @Validate(required = true)
  private String password;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  @DefaultHandler
  public Resolution login() {
    String result = login(this.username, this.password);
    if (!result.equals(RESULT_LOGIN_SUCCESSFUL)) {
      ValidationError error = new SimpleError("Invalid login. Please try again");
      getContext().getValidationErrors().add("login_error", error);
      return new ForwardResolution("/index.jsp");
    } else {
      return new ForwardResolution("/QueryProjectGroups.action");
    }
  }

  @Override
  public String login(String name, String pass) {
    log.info("Attempting to login");
    if (null != name && null != pass && name.equals("admin") && pass.equals("admin123"))
      return RESULT_LOGIN_SUCCESSFUL;
    else
      return RESULT_LOGIN_FAILED;
  }
}
